import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '@/views/login.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'login',
    component: resolve => require(['../views/login'], resolve)
  },
  {
    path: '/',
    name: 'login',
    component: resolve => require(['../views/login'], resolve)
  },
  {
    path: '/index',
    name: 'index',
    component: resolve => require(['../views/index'], resolve), 
    redirect: "/home",//默认显⽰的children地址
    children: [
      //管理员
      {
      path: '/home',
      name: '欢迎⻚',
      component: resolve => require(['../views/index/home'], resolve),
    }, {
      path: '/manager/class',
      name: '班级管理',
      component: resolve => require(['../views/manager/class'], resolve),
    },{
      path: '/manager/student',
      name: '学生管理',
      component: resolve => require(['../views/manager/student'], resolve),  
    },{
      path: '/manager/teacher',
      name: '学生管理',
      component: resolve => require(['../views/manager/teacher'], resolve),
    },
    //学生
    {
      path: '/student/course',
      name: '学生课表',
      component: resolve => require(['../views/student/course'], resolve),
    },
    //教师
    {
      path: '/teacher/course',
      name: '教师排课',
      component: resolve => require(['../views/teacher/course'], resolve),
    },
  ]
  }
];

const router = new VueRouter({
  routes,
});

//注册一个全局的路由守卫
router.beforeEach((to, from, next) => {
  if (to.path === "/login" || to.path === "/") {
    next();
  } else {
    //判断是否登录过    
    if (localStorage.getItem("token_key")) {
      next(); //放行
    } else {
      next({ path: "/login" }); //去见登陆页面
    }
  }
});

export default router;